1593E - Gardener and Tree - CodeForces Solution


brute force data structures dfs and similar greedy implementation trees *1600

Please click on ads to support us..

Python Code:

import sys

input = sys.stdin.readline
inf = float('inf')


def getInt():
    return int(input())


def getStr():
    return input().strip()


def getList(split=True):
    s = getStr()
    if split:
        s = s.split()
    return map(int, s)


t = getInt()


def solve():
    input()
    n, k = getList()
    from collections import deque

    q = deque()
    g = [[] for _ in range(n+1)]
    d = [0] * (n+1)
    for _ in range(n-1):
        u, v = getList()
        g[u].append(v)
        g[v].append(u)
    deg = list(map(len, g))
    for i in range(1, n+1):
        if len(g[i]) == 1:
            d[i] = 1
            q.append(i)

    while q:
        u = q.popleft()
        for v in g[u]:
            deg[v] -= 1
            if deg[v] == 1:
                d[v] = d[u]+1
                q.append(v)

    print(sum(d[i] > k for i in range(1, n+1)))


for _ in range(t):
    solve()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define ba boolalpha
#define F first
using vi = vector<int>;
using vll = vector<ll>;
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
vector<int> dx = {1, 1, -1, -1};
vector<int> dy = {1, -1, 1, -1};
const long long int mod = 1000000007;
#define loop(i,a,b) for(int i=a;i<b;i++)
ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
long long binpow(long long a, long long b, long long m) {  a %= m;long long res = 1; while (b > 0) { if (b & 1)res = res * a % m;a = a * a % m;  b >>= 1;} return res;}
long long sum_array(vector<ll>&ans){ll n = ans.size();ll x = 0;loop(i,0,n){x+=ans[i];}return x;}
ll phi(ll n){ll res = n;for(int i = 2;i*i<=n;i++){if(n%i==0){while(n%i==0){n=n/i;}res-=res/i;}}if(n>1)res=res-res/n;return res;}
ll extendedgcd(ll a, ll b, ll& x, ll& y){if(b==0){x=1;y=0;return a;}ll x1,y1; ll d = extendedgcd(b,a%b,x1,y1);x=y1;y=x1-y1*(a/b);return d;}
bool lineardiph(ll a,ll b,ll c,ll&x,ll&y,ll&g){g=extendedgcd(abs(a),abs(b),x,y); if(c%g!=0){return false;}; x*=c/g;y*=c/g;if(a<0){x=-x;}if(b<0){y=-y;}return true;}
void read(vll&a){loop(i,0,a.size())cin>>a[i];}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
vll pref_sum(vll&pref,vll&a){pref[0]=a[0];for(int i = 1;i<a.size();i++){pref[i]=pref[i-1]+a[i];}return pref;}
 long long NcR(int n, int r) {
    if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r)
    long long ans = 1;
    int i;
 
    for(i = 1; i <= r; i++) {
        ans *= n - r + i;
        ans /= i;
    }
 
    return ans;
}
void solve(){
   ll n,k;
   cin>>n>>k;
   
   ll in[n+1]={0};
 
    vector<ll>adj[n+1];
   queue<ll>v;
   vector<bool>mark(n+1,false);
   for(int i = 0;i<n-1;i++){
    ll x,y;
    cin>>x>>y;
    adj[x].push_back(y);
    adj[y].push_back(x);
    in[x]++;
    in[y]++;
   }
   for(int i = 1;i<n+1;i++){
        if(in[i]<=1){
             v.push(i);
             mark[i]=true;
        }
   }
   
   ll tot = n;
   while(!v.empty() and k--){
    auto t =  v.size();
    for(int i=0;i<t;i++){
        auto fst = v.front();
        --tot;
        v.pop();
        for(auto x : adj[fst]){
           in[x]--;
           if(in[x]<=1 and mark[x]==false){
             mark[x]=true;
             v.push(x);
           }
        }
    }
   }
   cout<<max(tot,0LL)<<endl;
  return;

}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t;
    cin>>t;
    //t = 1;
    // for(int i = 1;i<=t;i++){
    //     cout<<"Case #"<<i<<": ";
    //     solve();
    // }
    while(t--){
        solve();
    }
 
}


Comments

Submit
0 Comments
More Questions

189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books
1374B - Multiply by 2 divide by 6
1093B - Letters Rearranging
1213C - Book Reading
1468C - Berpizza
1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci